package cn.csust.net2.manager.client.ux;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import cn.csust.net2.manager.shared.po.PO;
import cn.csust.net2.manager.shared.service.BaseServiceAsync;

import com.extjs.gxt.ui.client.data.BaseListLoadConfig;
import com.extjs.gxt.ui.client.data.BaseListLoader;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelReader;
import com.extjs.gxt.ui.client.data.ListLoadResult;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class BeanCombo extends ComboBox<BeanModel> {

	private BaseListLoader<ListLoadResult<BeanModel>> loader;
	//过滤器，用来帅选Combo的数据
	private BaseListLoadConfig filter = new BaseListLoadConfig();
	
	public <G extends PO> BeanCombo(final PO po,final BaseServiceAsync service) {
		this.setDisplayField(po.getDisplayName());
		this.setTriggerAction(TriggerAction.ALL);
		RpcProxy<List<G>> beanProxy = new RpcProxy<List<G>>() {

			@Override
			protected void load(Object loadConfig,
					AsyncCallback<List<G>> callback) {
				// TODO Auto-generated method stub
				service.findAll(po.getAssist().getClassName(), (BaseListLoadConfig)loadConfig, callback);
			}
		};
		loader = new BaseListLoader<ListLoadResult<BeanModel>>(
				beanProxy, new BeanModelReader());
		ListStore<BeanModel> store = new ListStore<BeanModel>(loader);
		loader.load();
		this.setStore(store);
	}
	
	
	
	public <G extends PO> BeanCombo(final PO po,final BaseServiceAsync service, final BaseListLoadConfig load) {
		this.setDisplayField(po.getDisplayName());
		this.setTriggerAction(TriggerAction.ALL);
		filter = load;
		RpcProxy<List<G>> beanProxy = new RpcProxy<List<G>>() {

			@Override
			protected void load(Object loadConfig,
					AsyncCallback<List<G>> callback) {
				// TODO Auto-generated method stub				
				loadConfig = filter;
				service.findAll(po.getAssist().getClassName(), (BaseListLoadConfig)loadConfig, callback);
			}
		};
		loader = new BaseListLoader<ListLoadResult<BeanModel>>(
				beanProxy, new BeanModelReader());
		ListStore<BeanModel> store = new ListStore<BeanModel>(loader);
//		loader.load();
		this.setStore(store);
	}
	
	
	
	//条件加载接口
	public void load(Map<String,Object> config ){
		
		BaseListLoadConfig configs = (BaseListLoadConfig) loader.getLastConfig();
		if(configs==null) {
			configs = new BaseListLoadConfig();
		}
		Set<Entry<String, Object>> set = config.entrySet();
		for(Entry<String, Object> entry:set){
			configs.set(entry.getKey(), entry.getValue());
		}
		loader.load(configs);
	}
	

	public BeanModel findBeanModel(String property, String value) {
		BeanModel beanModel = this.findModel(property, value);
		return beanModel;
	}
	
	public BaseListLoader<ListLoadResult<BeanModel>> getLoader() {
		return loader;
	}

	public void setLoader(BaseListLoader<ListLoadResult<BeanModel>> loader) {
		this.loader = loader;
	}
	
	public BaseListLoadConfig getFilter() {
		return filter;
	}

	public void setFilter(BaseListLoadConfig filter) {
		this.filter = filter;
	}
}
